package com.herench.bunch.commons.support;


import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.segment.MSH;
import com.herench.bunch.commons.utils.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by ThinkPad on 2017/8/24.
 */

public abstract class AbstractMessageHandler implements MessageHandler {


    Logger logger = LoggerFactory.getLogger(this.getClass());


    private String msgCode;
    private String handlerName;
    private MessageConvert messageConvert;


    @Override
    public MessageConvert getMessageConvert() {
        return messageConvert;
    }

    @Override
    public void setMessageConvert(MessageConvert messageConvert) {
        this.messageConvert = messageConvert;
    }


    public String getMsgCode() {
        return msgCode;
    }

    public AbstractMessageHandler() {
    }



    @Override
    public ResponseMessage handler(RequestMessage requestMessage) throws Exception {
        Assert.notNull(this.getMsgCode(), "handler 的msgCode 为空");
        String funcode = requestMessage.getMsgCode();
        String captcha = requestMessage.getCaptcha();
        Assert.isTrue(signCheck(requestMessage), "签名验证失败");
        if (this.getMsgCode().equals(funcode)) {

            ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
            MessageConvert convert = getMessageConvert();
            Object inMessage = convert.toMessage(requestMessage);
            Object res = proxyInvoke(inMessage);
            Message hl7Message = convert.fromMessage(res);
            responseMessage.setHl7Message(hl7Message);
            Map<String, Object> data = responseMessage.getData();
            if (data != null) {
                String resMsgSign = MD5Util.string2MD5(data.toString());
                responseMessage.setDataAttribute("sign", resMsgSign);
            } else {
                responseMessage.setData(new HashMap<>());
            }

            if (hl7Message != null) {
                MSH msh = (MSH) hl7Message.get("MSH");
                msh.getMessageControlID().setValue(responseMessage.getId());
                msh.getSendingApplication().getHd1_NamespaceID().setValue("HIS");
                msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue(requestMessage.getMsgCode());
                msh.getReceivingApplication().getUniversalID().setValue(requestMessage.getId());
                msh.getReceivingApplication().getHd1_NamespaceID().setValue(requestMessage.getSender());
            }
            responseMessage.setReciver(requestMessage.getSender());
            return responseMessage;
        } else {
            throw new RuntimeException(this.getMsgCode() + "messageHandler,被错误的指定为:" + funcode + "处理");
        }
    }

    public AbstractMessageHandler(String msgCode, String handlerName) {
        this.msgCode = msgCode;
        this.handlerName = handlerName;
    }

    public void setMsgCode(String msgCode) {
        Assert.notNull(msgCode, "msgCode，不能为空.");
        this.msgCode = msgCode;
    }

    public void setHandlerName(String handlerName) {
        this.handlerName = handlerName;
    }

    public String getHandlerName() {
        return handlerName;
    }


    protected boolean signCheck(RequestMessage requestMessage) {
        return true;
    }

    protected void paramsCheck() {

    }

    protected abstract Object proxyInvoke(Object inMessage) throws Exception;
}
